package com.baby.work.domain.loan;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class Calculator {
    public static double total = 505729.48;
    private final static double rate = 0.05145;
    public static int period = 307;

    private final static double average = 1647.32;

    public static double shortenPeriod(int repayment) {
        double totalRemain = total - repayment;
        double totalRepayment = 0;
        int newPeriod = period - (int)(repayment / average);

        for(int i = 0; i < newPeriod; i++) {
            double interest = (totalRemain - average * i) * rate / 12;
            double monthRepayment = interest + average;
            if (repayment != 0) {
                System.out.println(monthRepayment);
            }
            totalRepayment += monthRepayment;
        }
        System.out.println("预计到 " + getDate(newPeriod) + " 还款结束; " + "当前到 " + getDate(period) + " 还款结束");
        return totalRepayment;
    }

    public static String getDate(int newPeriod) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(calendar.MONTH, newPeriod);
        date = calendar.getTime();
        return simpleDateFormat.format(date);
    }

    public static double notShortenPeriod(int repayment) {
        double totalRemain = total - repayment;
        double totalRepayment = 0;

        for(int i = 0; i < period; i++) {
            double interest = (totalRemain - average * i) * rate / 12;
            double monthRepayment = interest + average;
//            if (repayment != 0) {
//                System.out.println(monthRepayment);
//            }
            totalRepayment += monthRepayment;
        }
        return totalRepayment;
    }

    public static double test() {
        double totalRemain = 450000;
        double totalRepayment = 0;
        double average = totalRemain / 300;

        for(int i = 0; i < 300; i++) {
            double interest = (totalRemain - average * i) * rate / 12;
            double monthRepayment = interest + average;
//            if (repayment != 0) {
//                System.out.println(monthRepayment);
//            }
            totalRepayment += monthRepayment;
        }
        return totalRepayment;
    }

    public static void main(String[] args) {
        int payment = 90000;
        total = 440787.52;
        period = 267;

        System.out.println("提前还款 " + payment + "万 减少时间方式，总共减少本息： " + (shortenPeriod(0) - shortenPeriod(payment)));
//        System.out.println("提前还款 " + payment + "万 每月减少方式，总共减少本息： " + (notShortenPeriod(0) - notShortenPeriod(payment)));
        System.out.println("目前总共本息: " + notShortenPeriod(0));
//        System.out.println("===== " + test());
    }



}
